package com.tencent.qcloud.tim.uikit.bean

import androidx.lifecycle.MutableLiveData
import org.greenrobot.eventbus.EventBus

/**
 *


 * @date  2020/7/17  16:26
 * @version 1.0
 */
const val FRIEND_TYPE_ALL = 0
const val FRIEND_TYPE_FANS = 1
const val FRIEND_TYPE_SHARE = 2

object InviteLiveData {


    val friendList: MutableLiveData<MutableList<MsgFriendBean>> by lazy {
        MutableLiveData<MutableList<MsgFriendBean>>()
    }

    val fansList: MutableLiveData<MutableList<MsgFriendBean>> by lazy {
        MutableLiveData<MutableList<MsgFriendBean>>()
    }

    val shareList: MutableLiveData<MutableList<MsgFriendBean>> by lazy {
        MutableLiveData<MutableList<MsgFriendBean>>()
    }


    fun initSplitData() {
        val list = friendList.value ?: mutableListOf()
        if (list.size<=0)
            return
        val data = list.groupBy {
            if (it.type == "1")
                "fansList"
            else
                "shareList"
        }
        fansList.value = data["fansList"] as MutableList<MsgFriendBean>?
        shareList.value = data["shareList"] as MutableList<MsgFriendBean>?
    }

    fun getPos(pos: Int, type: Int = FRIEND_TYPE_ALL): MsgFriendBean? {

        val list = when (type) {
            FRIEND_TYPE_FANS -> fansList.value ?: mutableListOf()
            FRIEND_TYPE_SHARE -> shareList.value ?: mutableListOf()
            else -> friendList.value ?: mutableListOf()
        }
        if (list.size > pos) {
            return list[pos]
        }
        return null
    }

    fun getCheckNum(type: Int = FRIEND_TYPE_ALL): Int {
        val list = when (type) {
            FRIEND_TYPE_FANS -> fansList.value ?: mutableListOf()
            FRIEND_TYPE_SHARE -> shareList.value ?: mutableListOf()
            else -> friendList.value ?: mutableListOf()
        }
        return list.count { it.isChecked }
    }

    fun getCheckIds(type: Int = FRIEND_TYPE_ALL): String {
        var ids = ""
        var list = when (type) {
            FRIEND_TYPE_FANS -> fansList.value ?: mutableListOf()
            FRIEND_TYPE_SHARE -> shareList.value ?: mutableListOf()
            else -> friendList.value ?: mutableListOf()
        }
        list = list.filter { it.isChecked }.toMutableList()
        for (bean in list) {
            if (ids.isEmpty())
                ids = bean.uid
            else
                ids += ",${bean.uid}"
        }
        return ids
    }

    fun addData(data: MsgFriendBean, type: Int = FRIEND_TYPE_ALL) {
        val list = when (type) {
            FRIEND_TYPE_FANS -> fansList.value ?: mutableListOf()
            FRIEND_TYPE_SHARE -> shareList.value ?: mutableListOf()
            else -> friendList.value ?: mutableListOf()
        }
        list.add(data)
        when (type) {
            FRIEND_TYPE_FANS -> fansList.value = list
            FRIEND_TYPE_SHARE -> shareList.value = list
            else -> friendList.value = list
        }
    }

    fun addData(data: MutableList<MsgFriendBean>, type: Int = FRIEND_TYPE_ALL) {
        when (type) {
            FRIEND_TYPE_FANS -> fansList.value = data
            FRIEND_TYPE_SHARE -> shareList.value = data
            else -> friendList.value = data
        }
    }


    fun doSearch(key: String, type: Int = FRIEND_TYPE_ALL): MutableList<MsgFriendBean> {
        val list = when (type) {
            FRIEND_TYPE_FANS -> fansList.value ?: mutableListOf()
            FRIEND_TYPE_SHARE -> shareList.value ?: mutableListOf()
            else -> friendList.value ?: mutableListOf()
        }
        return list.filter { it.userNickname.contains(key) || it.serialNo.contains(key) }.toMutableList()
    }

    fun changeEditStatus(boo: Boolean, type: Int = FRIEND_TYPE_ALL) {
        val list = when (type) {
            FRIEND_TYPE_FANS -> fansList.value ?: mutableListOf()
            FRIEND_TYPE_SHARE -> shareList.value ?: mutableListOf()
            else -> friendList.value ?: mutableListOf()
        }
        when (type) {
            FRIEND_TYPE_FANS -> fansList.value = list.map {
                it.isEdit = boo
                it
            }.toMutableList()
            FRIEND_TYPE_SHARE -> shareList.value = list.map {
                it.isEdit = boo
                it
            }.toMutableList()
            else -> friendList.value = list.map {
                it.isEdit = boo
                it
            }.toMutableList()
        }
    }

    fun unCheckAll(type: Int = FRIEND_TYPE_ALL) {
        val list = when (type) {
            FRIEND_TYPE_FANS -> fansList.value ?: mutableListOf()
            FRIEND_TYPE_SHARE -> shareList.value ?: mutableListOf()
            else -> friendList.value ?: mutableListOf()
        }

        when (type) {
            FRIEND_TYPE_FANS -> fansList.value = list.map {
                it.isChecked = false
                it
            }.toMutableList()
            FRIEND_TYPE_SHARE -> shareList.value = list.map {
                it.isChecked = false
                it
            }.toMutableList()
            else -> friendList.value = list.map {
                it.isChecked = false
                it
            }.toMutableList()
        }
    }

    fun changeCheckStatus(pos: Int, type: Int = FRIEND_TYPE_ALL) {
        val list = when (type) {
            FRIEND_TYPE_FANS -> fansList.value ?: mutableListOf()
            FRIEND_TYPE_SHARE -> shareList.value ?: mutableListOf()
            else -> friendList.value ?: mutableListOf()
        }

        if (list.size > pos) {
            list[pos].isChecked = !list[pos].isChecked
            when (type) {
                FRIEND_TYPE_FANS -> {
                    fansList.value = list
                    EventBus.getDefault().post(GroupHChange())
                }
                FRIEND_TYPE_SHARE -> {
                    shareList.value = list
                    EventBus.getDefault().post(GroupHChange())
                }
                else -> friendList.value = list
            }
        }
    }


    fun clear(type: Int = FRIEND_TYPE_ALL) {
        when (type) {
            FRIEND_TYPE_FANS -> fansList.value = mutableListOf()
            FRIEND_TYPE_SHARE ->  shareList.value = mutableListOf()
            else ->  friendList.value = mutableListOf()
        }
    }

    fun clearAll() {
           fansList.value = mutableListOf()
           shareList.value = mutableListOf()
           friendList.value = mutableListOf()
    }


}